package org.example.common.core.model;

import lombok.Data;
import org.example.common.core.enums.ResultCode;

/**
 * @author : [Pashm]
 * @version : [v1.0]
 */
@Data
public class Result<T>{

    private Integer code;

    private String msg;

    private T data;//请求某个接口返回的数据


    public static <T> Result<T> ok(){
        return assembleResult(null, ResultCode.SUCCESS);
    }

    public static <T> Result<T> ok(T data){
        return assembleResult(data, ResultCode.SUCCESS);
    }

    public static <T> Result<T> fail(){
        return assembleResult(null, ResultCode.FAILED);
    }

    public static <T> Result<T> fail(int code, String msg) {
        return assembleResult(code, msg, null);
    }
    private static <T> Result<T> assembleResult(int code, String msg, T data) {
        Result<T> r = new Result<>();
        r.setCode(code);
        r.setData(data);
        r.setMsg(msg);
        return r;
    }



    /**
     * 指定错误码
     *
     * @param resultCode 指定错误码
     * @param <T>
     * @return
     */
    public static <T> Result<T> fail(ResultCode resultCode){
        return assembleResult(null, resultCode);
    }
    private static <T> Result<T> assembleResult(T data, ResultCode resultCode) {
        Result<T> result = new Result<>();
        result.setCode(resultCode.getCode());
        result.setData(data);
        result.setMsg(resultCode.getMsg());
        return result;
    }

}
